package com.atguigu.srb.core.controller.admin;


import com.atguigu.guigu.common.result.R;
import com.atguigu.srb.core.pojo.entity.Lend;
import com.atguigu.srb.core.service.DictService;
import com.atguigu.srb.core.service.LendService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的准备表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2022-11-19
 */
@RestController
@RequestMapping("/admin/core/lend")
public class AdminLendController {
    @Resource
    LendService lendService;
    @Resource
    DictService dictService;
    @ApiOperation("标的满标放款")
    @PutMapping("/makeLoan/{id}")
    public R makeLoan(@PathVariable("id")String id){
        //放款业务在此方法中直接完成
        lendService.makeLoan(id);
        return R.ok();
    }
    @ApiOperation("查询标的详情")
    @GetMapping("{id}")
    public R lend(@PathVariable("id")String id){
        Map<String,Object> map = lendService.getLend(id);
        return R.ok().data(map);
    }
    @ApiOperation("修改标的状态")
    @PutMapping("status/{id}/{status}")
    public R updateLendStatus(@PathVariable("id")String id,
                              @PathVariable("status")Integer status){
        LambdaUpdateWrapper<Lend> wrapper = Wrappers.lambdaUpdate(Lend.class)
                .eq(Lend::getId, id)
                .set(Lend::getStatus, status);
        if(status==1){
            wrapper.set(Lend::getPublishDate , new Date());
        }
        lendService.update(wrapper);
        return R.ok();
    }
    @ApiOperation("查询标的列表")
    @GetMapping
    public R list(){
        List<Lend> list = lendService.list();
        //对集合中的lend对象的status和 还款方式进行处理方便前端展示
        list.forEach(lend -> {
            Integer status = lend.getStatus();
            String statusStr = lendService.getStatusString(status);
            lend.getParam().put("status",statusStr);
            String name = dictService.getDictNameByDictCodeAndValue("returnMethod", lend.getReturnMethod());
            lend.getParam().put("returnMethod",name);
        });

        return R.ok().data("items",list);
    }
}

